package org.tobuy.task;


	import java.sql.Timestamp;
	import java.util.ArrayList;
	import java.util.Iterator;
	import java.util.List;
	import java.util.TimerTask;
	import org.tobuy.vo.*;
	import org.tobuy.factory.*;

	public class ScanTask extends TimerTask{

		@Override
		public void run() {
			System.out.print("run");
			List<Auction> auctions = new ArrayList<Auction>();
			try {
				auctions = AuctionDAOFactory.getIAuctionDAOInstance().findAuctions();
			} catch (Exception e) {
				e.printStackTrace();
			}
			Iterator<Auction> auctionIter = auctions.iterator();
			while(auctionIter.hasNext()){
				Auction auction = auctionIter.next();
				Timestamp now = new Timestamp(System.currentTimeMillis());
				if(now.after(auction.getExp_time())){
					if(auction.getBuyer_id()==null){
						try {
							AuctionDAOFactory.getIAuctionDAOInstance().deleteAuction(Integer.toString(auction.getItem_id()));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}else{
						Deal deal = new Deal();
						deal.setBuyer_id(auction.getBuyer_id());
						deal.setDeal_price(auction.getBuyer_offer());
						deal.setItem_id(auction.getItem_id());
						deal.setSeller_id(auction.getSeller_id());
						deal.setDeal_date(new Timestamp(System.currentTimeMillis()));
						try {
							DealDAOFactory.getIDealDAOInstance().addDeal(deal);
							AuctionDAOFactory.getIAuctionDAOInstance().deleteAuction(Integer.toString(auction.getItem_id()));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
				
			}
		}

	}

